package cn.wolfcode.trip.admin.controller;


/**
 * Created by 13925 on 2018/10/29.
 */

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 后台地区管理层
 */
@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {

        return "region/list";
    }

    @RequestMapping("/getRegionByParentId")
    @ResponseBody
    public Object getRegionByParentId(String parentId) {
        List<Region> list = regionService.getRegionByParentId(parentId);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (Region region : list) {
            mapList.add(region.toMap());
        }
        return mapList;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Region region) {
        regionService.saveOrUpdate(region);
        return "redirect:/region/list.do";
    }
}
